var appIndex = angular.module('boardApp', ['ngRoute'])
    .config(['$routeProvider', function($routeProvider){
        $routeProvider.when('/home', {//首页
            templateUrl: 'template-home.html',
            controller:  'homeController',
            activeSideTab: 'home'
        });
        $routeProvider.when('/user', {//用户
            templateUrl: 'template-user.html',
            controller:  'userController',
            activeSideTab: 'user'
        });
        $routeProvider.otherwise({
            redirectTo: '/home'
        });
    }]);

appIndex.controller('userController', function ($scope, $http) {
    $http.get('/users/login').success(function(login) {
        if (login.error) disconnectSocket();
        else $scope.userinfo = login;
    });
    $scope.list = [];
    var refresh = function () {
        $http.get('/users/all').success(function (msg) {
            $scope.list = msg;
        }).error(function (e) {
            erroralert(e.result);
        });
    };
    refresh();
    $scope.checkEdit = function () {
        var check_count = 0;
        $scope.checked = null;
        $scope.list.forEach(function (a) {
            if(a.checked) {
                $scope.checked = a.username;
                check_count++;

            }
        });
        return check_count != 1;
    };
    $scope.reset = function () {
        $scope.checked = null;
        refresh();
    };
    $scope.editting = false;
    $scope.editBtn = function () {
        if($scope.checked){
            $http.get('/users/detail/'+$scope.checked).success(function (msg) {
                $scope._f = msg;
                $scope.editting = true;
            }).error(function (e) {
                erroralert(e.result);
            })
        }else{
            $scope._f = {};
            $scope.editting = true;
        }
    };
    $scope.submit = function () {
        if($scope.checked){
            $http.put('/users/'+$scope.checked, $scope._f).success(function () {
                refresh();
                messagedelay('人员“'+$scope._f.username+'”编辑成功！');
                $scope.cancel();
            }).error(function (e) {
                erroralert(e.result);
            })
        }else{
            $http.post('/users', $scope._f).success(function () {
                refresh();
                messagedelay('人员“'+$scope._f.username+'”新建成功！');
                $scope.cancel();
            }).error(function (e) {
                erroralert(e.result);
            })
        }
    };
    $scope.cancel = function () {
        $scope.checked = null;
        $scope._f = {};
        $scope.editting = false;
    };
    $scope.delete = function () {
        if($scope.checked){
            confirmOut('删除人员“'+$scope.checked+'”?',function () {
                $http.delete('/users/'+$scope.checked).success(function () {
                    refresh();
                    messagedelay('人员“'+$scope.checked+'”删除成功!');
                    $scope.checked = null;
                }).error(function (e) {
                    erroralert(e.result);
                })
            })
        }
    };
});

appIndex.controller('navbarController', function ($scope, $http, $location, $route) {
    $scope.$route = $route;
    $scope.logout = function () {
        $('#navbar-collapse').collapse('hide');
        $http.get('/users/logout');
    };
    $http.get('/users/login').success(function(login) {
        if(login.error) {
            erroralert(login.result);
            location.assign('/login');
        }else $scope.userinfo = login;
    });
});

appIndex.controller('homeController', function () {

});